// components/StIndexes/StIndexes.js
const app = getApp();
Component({
    options:{
        multipleSlots: true, //多slot支持
        addGlobalClass: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        brandList:{
            type: Object,
            value: {}
        },
        showBack:{
            type: Boolean,
            value: false
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        hidden: true,
        list:['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'],
        listCur:'A',
        searchValue:'',
        showKey:{
            'A':true,'B':true,'C':true,'D':true,'E':true,'F':true,'G':true,'H':true,'I':true,'J':true,'K':true,'L':true,'M':true,'N':true,'O':true,'P':true,'Q':true,'R':true,'S':true,'T':true,'U':true,'V':true,'W':true,'X':true,'Y':true,'Z':true
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 品牌块的点击方法
        _blockClick(e){
            this.triggerEvent('itemClick', e.currentTarget.dataset.brand);
        },
        _back(){
            this.triggerEvent('back');
        },
        //获取文字信息
        getCur(e) {
            this.setData({
                hidden: false,
                listCur: this.data.list[e.target.id],
            })
        },
        // 内部过滤
        brandListSearch(){
            app.showLoading("正在搜索");
            let brandList = app.deepCopy(this.data.brandList);
            let showKey = app.deepCopy(this.data.showKey);
            let searchValue = this.data.searchValue;
            for (const key in brandList) {
                if (Object.hasOwnProperty.call(brandList, key)) {
                    const element = brandList[key];
                    let showTheKey = false;
                    element.forEach(element => {
                        if("" == searchValue){
                            element['show'] = 1;
                            showTheKey = true;
                        }else{
                            if(element.brandName.indexOf(searchValue) != -1){
                                element['show'] = 1;
                                showTheKey = true;
                            }else{
                                element['show'] = 0;
                            }
                        }
                    });
                    showKey[key] = showTheKey;
                }
            };
            wx.hideLoading();
            this.setData({
                brandList: brandList,
                showKey: showKey
            })
        },
        bindtapInput(e){
            this.setData({
                searchValue :e.detail.value
            })
        },
        setCur(e) {
            this.setData({
            hidden: true,
            listCur: this.data.listCur
            })
        },
        //滑动选择Item
        tMove(e) {
            let y = e.touches[0].clientY,
            offsettop = this.data.boxTop,
            that = this;
            //判断选择区域,只有在选择区才会生效
            if (y > offsettop) {
            let num = parseInt((y - offsettop) / 20);
            this.setData({
                listCur: that.data.list[num]
            })
            };
        },

        //触发全部开始选择
        tStart() {
            this.setData({
            hidden: false
            })
        },

        //触发结束选择
        tEnd() {
            this.setData({
            hidden: true,
            listCurID: this.data.listCur
            })
        },
        indexSelect(e) {
            let that = this;
            let barHeight = this.data.barHeight;
            let list = this.data.list;
            let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
            for (let i = 0; i < list.length; i++) {
            if (scrollY < i + 1) {
                that.setData({
                listCur: list[i],
                movableY: i * 20
                })
                return false
            }
            }
        },
    },
    lifetimes:{
        ready(){
            let that = this;
            let query = wx.createSelectorQuery().in(this);
            query.select('.indexBar-box').boundingClientRect(function(res) {
                that.setData({
                    boxTop: res.top
                })
            }).exec();
            
            query.select('.indexes').boundingClientRect(function(res) {
                that.setData({
                    barTop: res.top
                })
            }).exec()
        },
        created(){
        },
    }
})
